﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTMenus : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadLanguage(Thread.CurrentThread.CurrentCulture);
                    GetDanhMucMenus();           

            }
           
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbAddNew.Text = rm.GetString("lbAddNew", ci);
            lbDel.Text = rm.GetString("lbDel", ci);
            lbEdit.Text = rm.GetString("lbEdit", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbMenus.Text = rm.GetString("lbMenus", ci);
                  

        }

        public void GetDanhMucMenus()
        {
            Int32 TypeMenu=-1;
            MembershipUser theUser = Membership.GetUser(Session["UserName"].ToString());
            String roleName = Roles.GetRolesForUser(theUser.UserName)[0];
            if (roleName == HangSo.ROLE_ADMIN)
                TypeMenu = -1;
            else
                TypeMenu = 2;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                rpDanhMucMenu.DataSource = DanhMucMenusProvider.GetDanhMucMenus(TypeMenu);
                rpDanhMucMenu.DataBind();
            }
            else
            {
                rpMenuOther.DataSource = DanhMucMenusProvider.GetDanhMucMenus(TypeMenu);
                rpMenuOther.DataBind();
            }
        }

        public void CheckAll(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpDanhMucMenu.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpDanhMucMenu.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
            else
            {
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpMenuOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpMenuOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpDanhMucMenu.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        if (DanhMucMenusProvider.GetDanhMucMenusByID(Int32.Parse(lbDelID.Text)).NoDeleted)
                        {
                            
                                ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_Erorr + " " + DanhMucMenusProvider.GetDanhMucMenusByID(Int32.Parse(lbDelID.Text)).TitleOrder);
                            
                            return;
                        }
                        else
                        {
                            arr.Add(lbDelID.Text);
                        }
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpMenuOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        if (DanhMucMenusProvider.GetDanhMucMenusByID(Int32.Parse(lbDelID.Text)).NoDeleted)
                        {
                           
                                ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Del_ErorrOther + " " + DanhMucMenusProvider.GetDanhMucMenusByID(Int32.Parse(lbDelID.Text)).TitleOrder);
                            return;
                        }
                        else
                        {
                            arr.Add(lbDelID.Text);
                        }
                    }
                }
            }


            for (int i = 0; i < arr.Count; i++ )
                DanhMucMenusProvider.DeleteDanhMucMenus(Int32.Parse(arr[i].ToString())); 
            GetDanhMucMenus();
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            Int32 ID = 0;
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpDanhMucMenu.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpMenuOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }


            if (Count > 1 || Count == 0)
            {
                String messa = "";
                if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
                    messa = HangSo.Eorr_Edit;
                else
                    messa = HangSo.Eorr_EditOther;
                ltrErorr.Text = HelpMethod.ErorrLogin(messa);
            }
            else
            {
                Response.Redirect("EHTMenusUI.aspx?ID=" + ID.ToString());
            }
        }

        
    }
    
}